package cn.zzp.es.esDemo;

import cn.zzp.utils.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import javax.crypto.Cipher;
import java.io.Serializable;
import java.util.HashMap;

/**
 * @ClassName ESTest
 * @Description
 * @Author 祝志鹏
 * @Date 2022/8/17 22:39
 */
public class ESTest {

    /**
     * 添加数据
     */
    @Test
    public void add() {
        TransportClient client = ESClientUtil.getClient();

        HashMap<String, Object> map = new HashMap<>();

        map.put("name", "张三");
        map.put("age", "30");
        map.put("sex", "true");

        IndexRequestBuilder builder = client.prepareIndex("org", "emp", "1");

        IndexRequestBuilder source = builder.setSource(map);

        System.out.println(source.get());

        client.close();
    }

    /**
     * 查询
     */
    @Test
    public void search() {
        TransportClient client = ESClientUtil.getClient();

        SearchRequestBuilder org = client.prepareSearch("org");
        System.out.println(org.get());

        GetRequestBuilder getRequestBuilder = client.prepareGet("org", "emp", "1");
        System.out.println(getRequestBuilder.get());
    }

    @Test
    public void update() {
        TransportClient client = ESClientUtil.getClient();

        HashMap<String, Object> map = new HashMap<>();

        map.put("name", "李四");
        map.put("age", "28");

        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("org", "emp", "1");

        UpdateRequestBuilder doc = updateRequestBuilder.setDoc(map);

        System.out.println(doc.get());

        client.close();
    }

    /**
     * 删除
     */
    @Test
    public void testDel() {
        TransportClient client = ESClientUtil.getClient();

        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("org", "emp", "1");

        System.out.println(deleteRequestBuilder.get());
        client.close();
    }

    /**
     * @Description: 批量添加数据
     * @Author: Director
     * @Date: 2022/7/23 0:02
     * @return: void
     **/
    @Test
    public void testBatch() throws Exception {
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        // 3.批量创建文档，放入批量操作对象中
        for (int i = 2; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("org", "emp", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "我在源码学习Java!" + i)
                            .field("age", i - 1)
                            .field("sex", i % 2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        // 4.关闭连接
        client.close();
    }


     /*
       条件
        - 查询org表
        - name包含：我在源码
        - age在1~12之间
        - sex=1
        - 每页大小2
        - 从2页开始查
        - 按照age倒序
    */

    /**
     * 按条件查询
     */
    @Test
    public void query() {
        // 获取连接
        TransportClient client = ESClientUtil.getClient();
        // 指定搜索对象
        SearchRequestBuilder search = client.prepareSearch("org");
        // 设置搜索类型
        search.setTypes("emp");
        // 获取boolbuilder,并且设置搜索参数，作为参数传给query
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("name", "我在源码"));
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("age").lte(12).gte(1))
                .filter(QueryBuilders.termQuery("sex", "1"));

        // 设置query搜索条件
        search.setQuery(boolQueryBuilder);

        search.setSize(2);
        search.setFrom(2);
        search.addSort("age", SortOrder.DESC);

        SearchHit[] hits = search.get().getHits().getHits();
        for (SearchHit hit : hits) {

            System.out.println(hit.getSourceAsMap());
        }

        client.close();
    }

}
